﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS2011
{
    public partial class UnavailableTabControl : UserControl
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public UnavailableTabControl()
        {
            InitializeComponent();
        }

        string fileName = null;
        Process process = null;
        private void DownloadAndInstall()
        {
            try
            {
                linkDownloadAndInstall.Enabled = false;

                ServiioDownloadForm df = new ServiioDownloadForm();
                fileName = df.Download();
                if (!string.IsNullOrEmpty(fileName))
                {
                    process = User32Helper.StartChildProcess(this.ParentForm, fileName, "", false, true);
                    if (process != null)
                        process.Exited += process_Exited;
                }
                else
                {
                    linkDownloadAndInstall.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error Downloading / Installing Serviio", MessageBoxButtons.OK, MessageBoxIcon.Error);
                log.Error(ex);

                if (process != null)
                    process.Kill();

                linkDownloadAndInstall.Enabled = true;
            }
        }
        void process_Exited(object sender, EventArgs e)
        {
            try
            {
                if (process.ExitCode == 0)
                    MessageBox.Show("Serviio successfully installed. Please close this message and wait a few seconds for the plug-in to detect the service.", "Successfully Installed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    log.Warn("Install failed with exit code " + process.ExitCode);

                if (!string.IsNullOrEmpty(fileName) && System.IO.File.Exists(fileName))
                    System.IO.File.Delete(fileName);

                linkDownloadAndInstall.Enabled = true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                linkDownloadAndInstall.Enabled = true;
            }
        }

        private void UnavailableTabControl_Resize(object sender, EventArgs e)
        {
            panelCenter.Left = this.Width / 2 - panelCenter.Width / 2;
        }

        private void linkDownloadAndInstall_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DownloadAndInstall();
        }

        private void linkManagedServer_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                ChangeServerAddressForm dialog = new ChangeServerAddressForm();
                if (dialog.ChangeServerAddress())
                    MessageBox.Show("You must restart the Dashboard for any changes to take effect.");
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

    }
}
